/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Params;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.JOptionPane;

/**
 *
 *
 */
public class ThreadConnectionSocketSendFile extends Thread implements Runnable {

    public SocketListener socketListener;
    public Socket connectionSocket;
    public ObjectInputStream inFromServer;//nhap du lieu tu client goi
    public ObjectOutputStream outToServer;//xuat du lieu(len mang) den client 

    public ThreadConnectionSocketSendFile(Socket socket, SocketListener socketListener) {
        this.connectionSocket = socket;
        this.socketListener = socketListener;
        try {
            this.outToServer = new ObjectOutputStream(connectionSocket.getOutputStream());
            this.outToServer.flush();//xa het nhung j con trong luong ra ngoai
            this.inFromServer = new ObjectInputStream(connectionSocket.getInputStream());
        } catch (Exception e) {
            System.out.println(e);
        }
        start();//goi thread chay
    }
    @Override
    public void run(){
        try {
                int intFlag = 0;
                String strFileName = "";
                while(true){
                    Object objRecieved = inFromServer.readObject();
                    switch(intFlag){
                        case 0:
                            if (objRecieved.equals("IsFileTransfered")) {
                                intFlag++;
                            }
                            break;
                        case 1:
                            strFileName = (String) objRecieved;
                            int intOption = JOptionPane.showConfirmDialog(null,
                                    "Server is sending you "+
                                    strFileName+"!\nDo you want to recieve it?","Recieve Confirm",
                                    JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                            if (intOption == JOptionPane.YES_OPTION) {
                                intFlag++;
                            } else {
                                intFlag = 0;
                            }
                            break;
                       case 2:
                            byte[] arrByteOfReceivedFile = (byte[])objRecieved;
                            File file = new File("D:/sendfile.docx");
                            if(!file.exists()){
                                file.createNewFile();
                            }
                            //FileOutputStream outToHardDisk = new FileOutputStream(strFileName); 
                            FileOutputStream outToHardDisk = new FileOutputStream(file); 
                            outToHardDisk.write(arrByteOfReceivedFile);
                            
                            outToHardDisk.close();
                            intFlag = 0; 
                            JOptionPane.showMessageDialog(null,"File Recieved!",
                                    "Confirmation",JOptionPane.INFORMATION_MESSAGE);
                            break;
                    }
                    Thread.yield();
                }

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
